(()=>{
    let ele = {};

    ele.reduce = $('#reduce')//减少
    ele.add = $('#add')//增加
    ele.enternum = $('#enternum')//输入的数量

    ele.qi = $('#qi');//期数

    ele.lost_num = $('#lost_num');//剩余人数
    ele.need_num = $('#need_num');//总需要人次
    ele.prosess = $('#prosess');//进度条

    ele.list = $('#list');
    ele.surenum = $('#surenum');//确认按钮的数值
    ele.soha = $('#soha');//开始下注
    ele.mapname = $('#mapname');

    ele.mycion = $('#mycion');
    // 输入框
    let nums = parseFloat(ele.enternum.value)   

    let kiddata = ''; //详情数据
    let activenum = 0; // 选择的数据
    ele.mapimg = $('#mapimg');

    let id = getUrlParam('id')
    getdetail();
    init()

    ele.mapimg.src = `http://img.qxgs.cc/tiantianlaishouzu/img/seize/${id}.png`


    function init(){
        getUserInfo((res)=>{
            ele.mycion.innerHTML = res.diamond
        })
    }
    

    // 获取详情
    function getdetail(activenum){
        let token = getCookie("token");
		ajax({
			url: API_PATH.TREASUREDETAIL,
			method: 'GET',
			header: {
				'Authorization': token,
			},
            data: {
                id: id,
            },
			success: res => {
				if (res.code == 1) {
                    drawinfo(res.data , activenum)
                    kiddata = res.data
                    nums = Number(res.data.detail.lock_num) 
                    ele.enternum.value = nums
                    ele.surenum.innerHTML = '×' + nums
					ele.qi.innerHTML = '第' + res.data.qi + '期数'
                    ele.lost_num.innerHTML = '剩余人次:' + res.data.lost_num
                    ele.need_num.innerHTML = '总需要人次' + res.data.detail.need_num
                    ele.mapname.innerHTML = res.data.detail.name + '×' + res.data.detail.num

                    ele.add.onclick = () => {
                        if( Number(nums) > Number(res.data.lost_num)) {
                            nums = Number(res.data.lost_num)
                            ele.enternum.value = nums
                            ele.surenum.innerHTML = '×' + nums
                        }else if(Number(ele.enternum.value) + Number(res.data.detail.lock_num) > Number(res.data.lost_num)) {
                            console.log(ele.enternum.value + res.data.detail.lock_num);
                            nums = Number(res.data.lost_num)
                            ele.enternum.value = nums
                            ele.surenum.innerHTML = '×' + nums
                        }else {
                            if(!nums) {
                                nums = 1
                            }
                            nums= Number(nums) + Number(res.data.detail.lock_num)
                            ele.enternum.value = nums
                            ele.surenum.innerHTML = '×' + nums
                        }
                    }

                    // 减少
                    ele.reduce.onclick = () => {
                        nums =  Number(nums) - Number(res.data.detail.lock_num)
                        if (nums < Number(res.data.detail.lock_num)) {
                            nums = Number(res.data.detail.lock_num)
                            ele.enternum.value = nums
                            ele.surenum.innerHTML = '×' + nums
                            return
                        } else {
                            ele.enternum.value = nums
                            ele.surenum.innerHTML = '×' + nums
                        }
                    }

                    // 输入框输入
                    ele.enternum.oninput = () => {
                         if(!ele.enternum.value){
                            console.log(ele.enternum.value);
                            nums = 0
                            ele.surenum.innerHTML = '×' + nums
                        } else if(ele.enternum.value < 0) {
							nums = Number(res.data.detail.lock_num)
							ele.enternum.value = nums
							ele.surenum.innerHTML = '×' + nums
						}else if (ele.enternum.value > Number(res.data.lost_num)) {
                            openTips('投入数量不能大于剩余可投数量')
                            nums = res.data.lost_num
                            ele.enternum.value = nums
                            ele.surenum.innerHTML = '×' + nums
                            return
                        } else if (!/^\d{1,}$/.test(ele.enternum.value)) {
                            nums = parseInt(ele.enternum.value)
                            ele.enternum.value = nums
                            ele.surenum.innerHTML = '×' + nums
                            openTips('数量只能为整数')
                            return
                        }else {
                            nums = ele.enternum.value
                            ele.enternum.value = nums
                            ele.surenum.innerHTML = '×' + nums
                        }
                    }
				} else openTips(res.msg || '未知错误');
			}
		});
    }



    function drawinfo(data , activenum) {
        let html = ''
        if(activenum == 0) {
            ele.prosess.style.width = ((Number(data.detail.need_num) - Number(data.lost_num) ) / Number(data.detail.need_num)) * 100 +'%'
           
            let html2 = '<div class="item" data="'+data.lost_num+'">'+
                            '<span>剩余全部人次</span>'+
                            '<span>概率'+((data.lost_num / data.detail.need_num)*100).toFixed(1) +'%</span>'+
                        '</div>';
            for (let i = 0; i < data.detail.tj_num.length; i++) {
                let d = data.detail.tj_num[i]
                let odds = (Number(data.detail.tj_num[i].gl)  * 100).toFixed(1) + '%'
                html +=  '<div class="item" data="'+d.num+'">'+
                            '<span>'+d.num+'人次</span>'+
                            '<span>概率'+odds+'</span>'+
                        '</div>';
            }
            ele.list.innerHTML= html + html2
            choose()
        }else {
           let isactive2 = activenum == 4?'active':''
            let html2 = '<div class="item '+isactive2+'" data="'+data.lost_num+'">'+
                        '<span>剩余全部人次</span>'+
                        '<span>概率'+((data.lost_num / data.detail.need_num)*100).toFixed(1) +'%</span>'+
                    '</div>';
            ele.prosess.style.width = ((Number(data.detail.need_num) - Number(data.lost_num) ) / Number(data.detail.need_num)) * 100 +'%'

            for (let i = 0; i < data.detail.tj_num.length; i++) {
                let isactive = activenum-1 == i?'active':''
                let d = data.detail.tj_num[i]
                let odds = (Number(data.detail.tj_num[i].gl)  * 100).toFixed(1) + '%'
                html +=  '<div class="item '+isactive+'" data="'+d.num+'">'+
                            '<span>'+d.num+'人次</span>'+
                            '<span>概率'+odds+'</span>'+
                        '</div>';
            }
            ele.list.innerHTML= html + html2
            choose()
        }
    }

    let insert_num = 1

    // 选择按钮
    function choose(){
        let item = $('.item')
        for (let i = 0; i < item.length; i++) {
            item[i].onclick = function(){
                getdetail2(i+1)
               let choosenums =  this.getAttribute('data')
               if(Number(kiddata.lost_num) >= choosenums) {
                    this.classList.add('active')
                    nums = choosenums
                    ele.enternum.value = choosenums
                    ele.surenum.innerHTML = '×' + choosenums
                    insert_num = choosenums
                    for (let j = 0; j < item.length; j++) {
                        if(i !== j ) {
                            item[j].classList.remove('active')
                        }
                    }
               }else {
                    openTips('所选人次不足')
               }
            }
        }
    }



    // 获取详情
    function getdetail2(activenum){
        // console.log(activenum);
        let token = getCookie("token");
		ajax({
			url: API_PATH.TREASUREDETAIL,
			method: 'GET',
			header: {
				'Authorization': token,
			},
            data: {
                id: id,
            },
			success: res => {
				if (res.code == 1) {
                    drawinfo(res.data , activenum)
                    kiddata = res.data
					ele.qi.innerHTML = '第' + res.data.qi + '期数'
                    ele.lost_num.innerHTML = '剩余人次:' + res.data.lost_num
                    ele.need_num.innerHTML = '总需要人次' + res.data.detail.need_num
                    ele.mapname.innerHTML = res.data.detail.name

                    // if(activenum == 4){
                    //     nums = Number(res.data.lost_num)
                    //     ele.enternum.value = nums
                    //     ele.surenum.innerHTML = '×' + nums
                    // }else if( 0 < activenum < 4) {
                    //     nums = Number(res.data.detail.tj_num[activenum-1].num)
                    //     ele.enternum.value = nums
                    //     ele.surenum.innerHTML = '×' + nums
                    // }
				} else openTips(res.msg || '未知错误');
			}
		});
    }






    ele.soha.onclick = ()=>{
		if(nums <= 0){
			openTips('请输入正确的数量')
		}else {
			 insert(nums)
		}
    }

    // 开始下注按钮
    function insert(num){
        let token = getCookie("token");
        ajax({
			url: API_PATH.INSERT_COIN,
			method: 'GET',
			header: {
				'Authorization': token,
			},
            data: {
                id: id,
                num:num
            },
			success: res => {
				if (res.code == 1) {
                    openTips(res.msg)
                    getdetail2(0)
                    init()
				} else {
                    getdetail2(0)
                    openTips(res.msg || '未知错误');
                }
			}
		});
    }

})();